package com.zzl.study.auth.domain;

import lombok.Getter;
import lombok.Setter;

/**
 * @author: zhangzl
 * @date: 2023/12/7 18:24
 * @version: 1.0
 * @description: token设置
 * @see org.springframework.security.oauth2.server.authorization.settings.TokenSettings
 */
@Getter
public class TokenSettings {

    private static final String SETTINGS_NAMESPACE = "settings.";

    private static final String TOKEN_SETTINGS_NAMESPACE = SETTINGS_NAMESPACE.concat("token.");

    public static final String AUTHORIZATION_CODE_TIME_TO_LIVE = TOKEN_SETTINGS_NAMESPACE.concat("authorization-code-time-to-live");

    public static final String ACCESS_TOKEN_TIME_TO_LIVE = TOKEN_SETTINGS_NAMESPACE.concat("access-token-time-to-live");

    public static final String ACCESS_TOKEN_FORMAT = TOKEN_SETTINGS_NAMESPACE.concat("access-token-format");

    public static final String DEVICE_CODE_TIME_TO_LIVE = TOKEN_SETTINGS_NAMESPACE.concat("device-code-time-to-live");

    public static final String REUSE_REFRESH_TOKENS = TOKEN_SETTINGS_NAMESPACE.concat("reuse-refresh-tokens");

    public static final String REFRESH_TOKEN_TIME_TO_LIVE = TOKEN_SETTINGS_NAMESPACE.concat("refresh-token-time-to-live");

    public static final String ID_TOKEN_SIGNATURE_ALGORITHM = TOKEN_SETTINGS_NAMESPACE.concat("id-token-signature-algorithm");

    /**
     * 授权码有效时间
     */
    @Setter
    private long authorizationCodeTimeToLive = 5L;

    /**
     * 访问控制token的有效时间
     */
    @Setter
    private long accessTokenTimeToLive = 5L;

    /**
     * 创建访问控制token的格式：
     * 1、self-contained-完整模式（token中会返回完整的授权信息，无法更新，默认使用的就是此模式）；
     * 2、reference-引用模式（token只会返回对原始授权信息的引用，适用于动态修改授权信息而无需修改token，使用时需提供授权信息的查询接口）。
     */
    private final String accessTokenFormat = "self-contained";

    /**
     * 设备码有效时间
     */
    private final long deviceCodeTimeToLive = 5L;

    /**
     * 是否重用刷新token，否的话每次签发accessToken时都是一个新的refreshToken
     */
    private final boolean reuseRefreshTokens = true;

    /**
     * 刷新token有效时间
     */
    @Setter
    private long refreshTokenTimeToLive = 60L;

    /**
     * idToken（用户身份信息token）签名算法
     */
    private final String idTokenSignatureAlgorithm = "RS256";

    @Override
    public String toString() {
        return "{" +
                "\"@class\": \"java.util.Collections$UnmodifiableMap\"," +
                "\"" + REUSE_REFRESH_TOKENS + "\": " + this.reuseRefreshTokens + "," +
                "\"" + ID_TOKEN_SIGNATURE_ALGORITHM + "\": [\"org.springframework.security.oauth2.jose.jws.SignatureAlgorithm\", \"RS256\"]," +
                "\"" + ACCESS_TOKEN_TIME_TO_LIVE + "\": [\"java.time.Duration\", " + this.accessTokenTimeToLive * 60 + "]," +
                "\"" + ACCESS_TOKEN_FORMAT + "\": {\"@class\": \"org.springframework.security.oauth2.server.authorization.settings.OAuth2TokenFormat\", \"value\": \"" + this.accessTokenFormat + "\"}," +
                "\"" + REFRESH_TOKEN_TIME_TO_LIVE + "\": [\"java.time.Duration\", " + this.refreshTokenTimeToLive * 60 + "]," +
                "\"" + AUTHORIZATION_CODE_TIME_TO_LIVE + "\": [\"java.time.Duration\", " + this.authorizationCodeTimeToLive * 60 + "]," +
                "\"" + DEVICE_CODE_TIME_TO_LIVE + "\": [\"java.time.Duration\", " + this.deviceCodeTimeToLive * 60 + "]" +
                "}";
    }

}
